package com.yvdedu.hospital.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.yvdedu.hospital.entity.Department;
import com.yvdedu.hospital.entity.Drug;
import com.yvdedu.hospital.entity.Outpatient;
import com.yvdedu.hospital.entity.Prccs;
import com.yvdedu.hospital.entity.Presc;
import com.yvdedu.hospital.entity.Prescription;
import com.yvdedu.hospital.entity.Project;
import com.yvdedu.hospital.entity.Record;
import com.yvdedu.hospital.entity.User;
import com.yvdedu.hospital.entity.tool.OutpatientTool;
import com.yvdedu.hospital.entity.tool.PrccsTool;
import com.yvdedu.hospital.entity.tool.PrescTool;
import com.yvdedu.hospital.service.PharmacyService;
import com.yvdedu.hospital.service.RegistrationService;

@Controller
@RequestMapping("business/registration")
public class RegistrationController {
	
	@Resource
	RegistrationService registrationService;
	@Resource
	PharmacyService pharmacyService;
	@RequestMapping("process")
	public void process(Model model) {
		
	}
	
//	挂号管理
	@RequestMapping("management")
	public void management(Model model) {
		
		List<Outpatient> list = registrationService.outpatientquery();
		List<Department> li = registrationService.departmentquery();
//		System.out.println(li);
		model.addAttribute("outList",list);
		model.addAttribute("li",li);
	}

	
//	新建挂号
	@RequestMapping("domanagement")
	public Object domanagement(Outpatient outpatient,Model model) {
//		System.out.println(outpatient);
		
		//得到long类型当前时间
		long l = System.currentTimeMillis();
		//new日期对象
		Date date = new Date(l);
		//转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String sj = dateFormat.format(date);
		outpatient.setDat(sj);
		outpatient.setDeal("否");
		outpatient.setCases("否");
		System.out.println(outpatient);
//		
		registrationService.outpatientadd(outpatient);
		
		model.addAttribute("acc","true");
		model.addAttribute("url","management");
		return "info/tips";
		
	}
//  科室联动查询医生
	@RequestMapping("ks")
	@ResponseBody
	public Object ks(String pa) throws ServletException, IOException {
		System.out.println(pa);
		int dep_id = Integer.parseInt(pa);
		if(dep_id==1){
			System.out.println("-------------------");
		}else{
//			int dep_id = Integer.parseInt(pa);
			List<User> lis= registrationService.userquery(dep_id);
			System.out.println(lis);
			return lis;
		}
		return null;
		
		
	}
	
	//挂号查询
	@RequestMapping("search")
	public Object search(String name,Model model) {
		List<Outpatient> list = registrationService.searchoutpatient(name);
//		System.out.println(list);
		PageInfo page = new PageInfo(list);
		model.addAttribute("pageNumber",page.getPageNum());
		model.addAttribute("Pages",page.getPages());
		model.addAttribute("outList",list);
		return "business/registration/management";

	}
	@RequestMapping("balance")
	public void balance(Model model) {
		
	}
	
//	挂号管理导出Excel文件
	@RequestMapping("excel")
	public Object excel(int id[],String category[],String name[],String do_name[],String sex[],String fee[],String c_type[],String dat[],String deal[],HttpServletRequest request,Model model) throws FileNotFoundException, IOException{
		
//		System.out.println("---------------开始导出");
		
		
		//得到Excel工作簿对象   
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(request.getServletContext().getRealPath("/static/excel/model.xls")));
		
		//得到Excel工作表对象  
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		
		FileOutputStream fileOutputStream;
		
		HSSFSheet sheet = wb.getSheetAt(0);   
		//得到Excel工作表的行   
//		HSSFRow row = sheet.getRow(1);  
		//得到Excel工作表指定行的单元格   
//		HSSFCell cell = row.getCell((short) 1);
		
		for(int j=0; j<id.length; j++){
	//			创建新的一行
				HSSFRow rows = sheet.createRow(1+j);
	//			创建新的一列
				HSSFCell c = rows.createCell(0);
				c.setCellValue(id[j]);
				HSSFCell c1 = rows.createCell(1);
				c1.setCellValue(category[j]);
				HSSFCell c2 = rows.createCell(2);
				c2.setCellValue(name[j]);
				HSSFCell c3 = rows.createCell(3);
				c3.setCellValue(do_name[j]);
				HSSFCell c4 = rows.createCell(4);
				c4.setCellValue(sex[j]);
				HSSFCell c5 = rows.createCell(5);
				c5.setCellValue(fee[j]);
				HSSFCell c6 = rows.createCell(6);
				c6.setCellValue(c_type[j]);
				HSSFCell c7 = rows.createCell(7);
				c7.setCellValue(dat[j]);
				HSSFCell c8 = rows.createCell(8);
				c8.setCellValue(deal[j]);
		}
		
		
		
		fileOutputStream = new FileOutputStream("C:\\Users\\yvdedu.com\\Desktop\\hospital\\挂号管理.xls");
		wb.write(fileOutputStream);
		
		fileOutputStream.close();
		wb.close();
		fs.close();
		
//		System.out.println("导出结束---------------");
		model.addAttribute("data","导出成功，文件存放在桌面的hospital文件夹下！");
		model.addAttribute("acc","true");
		model.addAttribute("url","management");
		return "info/tips";
	}
	
//	医生开病例页面
	@RequestMapping("diagnosis")
	public void doctor(Model model) {
		//药品查询界面
		String dname="";
		int pageNumber = 1;
		List<Drug> list = registrationService.drugSerch(dname,pageNumber);
		//项目查询界面
		List<Project> li = registrationService.projectquery();
//		System.out.println(list);
		//挂号为就诊查询
		String deal="否";
//		System.out.println(deal);
		List<Outpatient> out = registrationService.outpatientque(deal);
// 病例为否的查询
		String cases="否";
		List<OutpatientTool> cas = registrationService.outpatientcases(cases);
		model.addAttribute("drug",list);
		model.addAttribute("project",li);
		model.addAttribute("out",out);
		model.addAttribute("cases",cas);
		PageInfo page = new PageInfo(list);
		model.addAttribute("pageNumber",page.getPageNum());
		model.addAttribute("Pages",page.getPages());
	}
	
//就诊页面传值
	@RequestMapping("chufang")
	public Object presc(int d_id[],int number[],int tory[], String dosage[],String usage[],String d_name[],int xd_id[],String xd_name[],int x_number[],Prescription prescription,Model model) {
		prescription.setCharge(1);
		System.out.println(prescription);
		int p_id=prescription.getId();
		registrationService.prescriptionadd(prescription);
		if(d_name!=null){
			for(int j=0; j<d_id.length; j++){
				Presc presc = new Presc();
				presc.setP_id(p_id);
				presc.setD_id(d_id[j]);
				presc.setNumber(number[j]);
				presc.setDosage(dosage[j]);
				presc.setUsagee(usage[j]);
				presc.setD_name(d_name[j]);
				System.out.println(presc);
				//药品添加
				registrationService.prescadd(presc);
				//药品跟新
				Drug drug = new Drug();
				drug.setId(d_id[j]);
				drug.setTory(tory[j]-number[j]);
				registrationService.drugedit(drug);
			}
		}
		
		if(xd_name!=null){
			for(int j=0; j<xd_id.length; j++){
				Prccs prccs = new Prccs();
				prccs.setXp_id(p_id);
				prccs.setXd_id(xd_id[j]);
				prccs.setXd_name(xd_name[j]);
				prccs.setX_number(x_number[j]);
				System.out.println(prccs);
				//项目添加
				registrationService.prccsadd(prccs);
			}
		}
		Outpatient outpatient = new Outpatient();
		outpatient.setId(p_id);
		outpatient.setDeal("是");
		registrationService.outpatientedit(outpatient);
		model.addAttribute("acc","true");
		model.addAttribute("url","diagnosis");
		
		return "info/tips";

	}
	
	//药品搜索添加
	@RequestMapping("drugserch")
	@ResponseBody
	public Object drugserch(String dname,int pageNumber,Model model) {
//		System.out.println(dname);
//		List<Drug> list = registrationService.drugSerch(dname,pageNumber);
//		PageInfo page = new PageInfo(list);
//		model.addAttribute("pageNumber",page.getPageNum());
//		model.addAttribute("Pages",page.getPages());
//		System.out.println(list);
//		return list;
		
		Map<String,Object> map = new HashMap<String,Object>();
		List<Drug> list = registrationService.drugSerch(dname,pageNumber);
		PageInfo page = new PageInfo(list);
		System.out.println(list);
		map.put("pageNmuber", page.getPageNum());
		map.put("pages", page.getPages());
		map.put("list", list);
		return map;
		
		
	}
	
	//项目搜索添加
	@RequestMapping("proserch")
	@ResponseBody
	public Object proserch(String xname) {
		System.out.println(xname);
		List<Project> list = registrationService.projectSerch(xname);
		return list;
	}
	
	//病例接受传值
	@RequestMapping("bladd")
	@ResponseBody
	public void doctbl( Record record,HttpServletResponse response ){
		System.out.println(record);
		response.setContentType("text/html;charset=UTF-8");
		try {
			response.getWriter().println("成功");
		} catch (IOException e) {
			e.printStackTrace();
		}
		registrationService.recordadd(record);
		int id = record.getId();
		Outpatient outpatient = new Outpatient();
		outpatient.setId(id);
		outpatient.setCases("是");
		registrationService.casesedit(outpatient);
	}
	//收费作废
	@RequestMapping("invalid")
	public void invalid(Model model) {
		
	}
	//收费搜索
	@RequestMapping("inserch")
	@ResponseBody
	public Object invalidserch(int id,int pageNaber) {
		System.out.println(id);
		System.out.println(pageNaber);
		List<PrccsTool> prccs = registrationService.serch1(id,pageNaber);
		List<PrescTool> presc = registrationService.serch(id,pageNaber);
		System.out.println(prccs);
		System.out.println(presc);
		Map<String,Object> map = new HashMap<String,Object>();
		PageInfo page = new PageInfo(prccs);
		PageInfo page1 = new PageInfo(presc);
		if(prccs==null){
			map.put("prccs", prccs);
			map.put("pageNmuber", page.getPageNum());
			map.put("pages", page.getPages());
		}
		if(presc==null){
			map.put("presc", presc);
			map.put("pageNmuber1", page1.getPageNum());
			map.put("pages1", page1.getPages());
		}
		if(presc!=null&&prccs!=null){
			map.put("prccs", prccs);
			map.put("presc", presc);
			if(page.getPages()>page1.getPages()){
				map.put("pageNmuber", page.getPageNum());
				map.put("pages", page.getPages());
			}else if (page.getPages()<page1.getPages()) {
				map.put("pageNmuber", page1.getPageNum());
				map.put("pages", page1.getPages());
			}
		}
		return map;
	}
	//确认收费作废
	@RequestMapping("indeit")
	@ResponseBody
	public Object invaliddeit(int id) {
		System.out.println(id);
		registrationService.intdeit(id);
		registrationService.intdeit1(id);
		registrationService.intdeit2(id);
		registrationService.intdeit3(id);
		String name = "删除成功";
		return name;
	}
	
}
